package com.ciwong.epaper.modules.epaper.ui;

import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.adapter.BookDeskGridAdapter;
import com.ciwong.epaper.modules.epaper.adapter.BookDeskTeacherAdapter;
import com.ciwong.epaper.modules.epaper.bean.GradeBean;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.homework.adapter.AttentionWorkAdapter;
import com.ciwong.epaper.modules.homework.bean.PublishWorkRecords;
import com.ciwong.epaper.modules.me.bean.FollowTeacher;
import com.ciwong.epaper.modules.me.bean.FollowTeacher;
import com.ciwong.epaper.modules.me.bean.PublishWorkRecordsPageBean;
import com.ciwong.epaper.modules.me.ui.BookTagActivity;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.widget.PullToRefreshView;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.widget.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我关注的作业
 * @description:老师关注的作业
 * @author: wyk
 * @time: 2021/6/24 10:46
 */
public class AttentionWorkActivity extends BaseActivity implements PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener, ExpandableListView.OnGroupClickListener,
        ExpandableListView.OnChildClickListener, View.OnClickListener {

    private ExpandableListView attention_work_ex_list;

    private PullToRefreshView my_work_refresh_view;

    private LinearLayout publish_work_lay;

    private NoScrollGridView grand_name_grid;

    private RelativeLayout handle_go_to_bookdesk;

    private AttentionWorkAdapter attentionWorkAdapter;
    //发布的作业列表
    private List<PublishWorkRecords> publishWorkRecords;

    private BookDeskTeacherAdapter bookDeskTeacherAdapter;
    /**
     * 默认加载数据大小
     */
    private static final int LOAD_PAGE_SIZE = 10;
    /**
     * 选择老师
     */
    private long teacherId = 0;

    /**
     * 当前页码
     */
    private int curPage = 1;

    //关注的老师列表
    private List<FollowTeacher> followTeacherList = new ArrayList<>();

    //老师列表选择器
    private List<FollowTeacher> chooseTeacherList = new ArrayList<>();

    @Override
    protected void preCreate() {
        followTeacherList = (List<FollowTeacher>) getIntent()
                .getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);
    }

    @Override
    protected int setView() {
        return R.layout.activity_teacher_attention_work;
    }

    @Override
    protected void findViews() {
        grand_name_grid = (NoScrollGridView) findViewById(R.id.grand_name_grid);
        my_work_refresh_view = findViewById(R.id.my_work_refresh_view);
        publish_work_lay = findViewById(R.id.publish_work_lay);
        handle_go_to_bookdesk = findViewById(R.id.handle_go_to_bookdesk);
        attention_work_ex_list = findViewById(R.id.attention_work_ex_list);
    }

    @Override
    protected void initEvent() {
        bookDeskTeacherAdapter = new BookDeskTeacherAdapter(AttentionWorkActivity.this, chooseTeacherList);
        grand_name_grid.setAdapter(bookDeskTeacherAdapter);
        grand_name_grid.setNumColumns(5);
        setNetGrade(followTeacherList);
    }

    @Override
    protected void init() {
        setTitleText(R.string.attention_work_title);
        attentionWorkAdapter = new AttentionWorkAdapter(AttentionWorkActivity.this, publishWorkRecords);
        attention_work_ex_list.setAdapter(attentionWorkAdapter);
        attention_work_ex_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
        my_work_refresh_view.setOnHeaderRefreshListener(this);
        my_work_refresh_view.setOnFooterRefreshListener(this);
    }

    @Override
    protected void loadData() {
        getFollowWorkRecords(curPage, teacherId);
    }


    /**
     * 填充网络数据
     *
     * @param followTeachers
     */
    public void setNetGrade(List<FollowTeacher> followTeachers) {
        chooseTeacherList.clear();
        FollowTeacher followTeacher = new FollowTeacher();
        followTeacher.setUserName("  全部");
        followTeacher.setUserId(0);
        followTeacher.setChecked(true);
        chooseTeacherList.add(followTeacher);
        teacherId = 0;
        for (int i = 0; i < followTeachers.size(); i++) {
            FollowTeacher item = followTeachers.get(i);
            FollowTeacher teacher = new FollowTeacher();
            teacher.setUserName(item.getUserName());
            teacher.setUserId(item.getUserId());
            teacher.setChecked(false);
            chooseTeacherList.add(teacher);
        }
        bookDeskTeacherAdapter.setNewData(chooseTeacherList);
    }


    /**
     * 获取关注的作业
     *
     * @param curPage
     */
    public void getFollowWorkRecords(int curPage, long teacherId) {
        this.teacherId = teacherId;
        this.curPage = curPage;
        showCricleProgress();
        EpaperDao.getInstance().getFollowWorkRecords(EApplication.BRAND_ID, teacherId,
                curPage, LOAD_PAGE_SIZE, new BaseExtCallBack(AttentionWorkActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                    @Override
                    public void failed(int errorCode, Object data) {
                        hideCricleProgress();
                        my_work_refresh_view.onFooterRefreshComplete();
                        my_work_refresh_view.onHeaderRefreshComplete();
                        super.failed(errorCode, data);
                        failed(data);
                    }

                    @Override
                    public void failed(Object data) {
                        hideCricleProgress();
                        my_work_refresh_view.onFooterRefreshComplete();
                        my_work_refresh_view.onHeaderRefreshComplete();
                        if (data instanceof TimeoutError) {
                            hideCricleProgress();
                            ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                        }
                    }

                    @Override
                    public void success(Object data) {
                        hideCricleProgress();
                        my_work_refresh_view.onFooterRefreshComplete();
                        my_work_refresh_view.onHeaderRefreshComplete();
                        if (null != data) {
                            PublishWorkRecordsPageBean publishWorkRecordsPageBean = (PublishWorkRecordsPageBean) data;
                            setWorkList(publishWorkRecordsPageBean);
                        }
                    }
                });
    }

    private void setWorkList(PublishWorkRecordsPageBean publishWorkRecordsPageBean) {
        List<PublishWorkRecords> pageList = publishWorkRecordsPageBean.getPageList();
        int totalCount = publishWorkRecordsPageBean.getTotalCount();
        if (0 == totalCount) {
            handle_go_to_bookdesk.setVisibility(View.VISIBLE);
            publish_work_lay.setVisibility(View.GONE);
        } else {
            publishWorkRecords = pageList;
            handle_go_to_bookdesk.setVisibility(View.GONE);
            publish_work_lay.setVisibility(View.VISIBLE);
            if (curPage == 1) {
                attentionWorkAdapter.clear();
                init();
            } else {
                attentionWorkAdapter.addAll(pageList);
            }
            for (int i = 0; i < attentionWorkAdapter.getGroupCount(); i++) {
                attention_work_ex_list.expandGroup(i);
            }

            if (attentionWorkAdapter.getGroupCount() == publishWorkRecordsPageBean.getTotalCount()) {
                my_work_refresh_view.setFooterState(PullToRefreshView.PULL_TO_NO_MORE);
            } else {
                my_work_refresh_view.setFooterState(PullToRefreshView.PULL_TO_REFRESH);
            }
        }

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
        return false;
    }

    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
        return false;
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        curPage = 1;
        getFollowWorkRecords(curPage, teacherId);
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        if (!NetworkUtils.isOnline()) {
            CWToast.makeText(AttentionWorkActivity.this, getString(R.string.connect_disable), Toast.LENGTH_LONG).setToastType(CWToast.TOAST_TYPE_ERROR).show();
            return;
        }
        curPage++;
        getFollowWorkRecords(curPage, teacherId);

    }
}
